<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/20
 * Time: 9:20
 */
namespace app\index\controller;

use think\Controller;
use think\Db;
use app\common\library\Auth;

class UserAction extends Base
{

    public $user_cash = null;
    public $finance_mol = null;
    public $user_bank = null;
    public $user_attribute = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->user_cash = new \app\common\model\user\Cash;
        $this->finance_mol = new \app\common\model\Finance;
        $this->user_attribute = new \app\common\model\user\Attribute;
        $this->user_bank = new \app\common\model\user\Bank;
    }


    //提现操作
    public function withdraw_act(){
        $uid  = $this->uid;
        $count = input("count");
        if(intval($count)<1){
            renderJson("提现金额有误",0);
        }
        $user_info = $this->user_model->where("uid",$uid)->field("balance,username")->find();
        $count = sprintf("%.2f",$count);
        if($user_info["balance"]<$count){
            renderJson("余额不足",0);
        }
        $balance = $user_info["balance"]- $count;
        $balance = sprintf("%.2f",$balance);
        Db::startTrans();
        try{
            $_cash = [
                "uid" => $uid,
                "cash_code"=>rand(0,99).get_only_order_no(),
                "bank_name"=>input("bank_name"),
                "bank_code"=>input("bank_code"),
                "bank_person"=>input("bank_person"),
                "cash"=>$count,
                "created_time"=>time(),
                "updated_time"=>time(),
                "status"=>1,
                "platform"=>1,
                "balance"=>$balance,
                "username"=>$user_info["username"],
            ];//提现信息
            $op = $this->user_cash->add($_cash);
            if($op){
                $_finance = [
                    "fina_type"=>"out",//提现属于支付
                    "obj_type"=>"withdraw",
                    "obj_id"=>$op,
                    "uid"=>$uid,
                    "username"=>$user_info["username"],
                    "desc"=>"余额提现-平台审核",
                    "order_id"=>"0",
                    "fina_cash"=>$count,
                    "fina_channel"=>1,
                    "balance"=>$balance,
                    "source"=>"paike",
                    "remark"=>"余额提现-平台审核",
                    "on_time"=>time(),
                ];


                $tp = $this->finance_mol->save($_finance);

                if($tp) {
                    Db::commit();
                    $this->user_model->where("uid",$uid)->setDec("balance",floatval($count));
                    renderJson("成功申请",1);
                }else{

                    Db::rollback();
                }


            }

        }catch (\Exception $e){

            renderJson("申请失败",0);

        }



    }


    //编辑个人信息操作
    public function user_info_act(){

        //halt(input());

        $data = input("post.");
        $uid = $this->uid;
        $input["avatar"] = $data["avatar_img"];
        $input["avatar"] = $data["avatar_img"];
        $input["avatar"] = $data["avatar_img"];
        $input["avatar"] = $data["avatar_img"];
        $input["avatar"] = $data["avatar_img"];
        $input["avatar"] = $data["avatar_img"];

        Db::name("user")->where("uid",$uid)->setField($input);

        renderJson("成功更改",1);

    }



    //密码修改
    public function reset_password(){
        $oldsw = input("oldsw");//旧密码
        $newpsw = input("newpsw");//新密码
        $confirm = input("confirm");//确认的密码
        $uid  = $this->uid;
        if($newpsw!=$confirm){
            renderJson("确认密码有误",0);
        }
        $user_info = $this->user_model->where('uid',$uid)->field("password,sec_code")->find();
        $auth = new Auth();
        $password = $auth->getEncryptPassword($oldsw, $user_info['sec_code']);
        if($password!=$user_info["password"]){
            renderJson("原密码有误",0);
        }
        $data["sec_code"] = $this->user_model->setSalt();
        $data["password"] = $auth->getEncryptPassword($newpsw,$data["sec_code"]);
        $this->user_model->save($data,['uid'=>$uid]);
        renderJson("成功设置",1);
    }




    //修改银行卡
    public function edit_bank(){

        $uid  = $this->uid;
        $data["bank_person"] = input("name");//持卡人
        $data["bank_name"] = input("bankname");//银行名称
        $data["bank_code"] = input("account");//银行卡号
        Db::startTrans();
        try{
            $op = $this->user_bank->save($data, ["uid" => $uid]);//修改
            if($op){
                $this->user_attribute->save(["bank_no"=>$data["bank_code"],"bank_name"=>$data["bank_name"]], ["uid" => $uid]);//修改银行卡号
                Db::commit();
                $openid = Db::name("wx_users")->where("uid",$uid)->value("openid");
                $msg = [
                    "condition"=>5,
                    "touser"=>$openid,
                    "content"=>[$this->userInfo["username"]]
                ];
                $MiniMsg = new  \app\common\lib\MiniMsg($msg);
                $MiniMsg->sendMsg();//发送短信模板银行卡修改成功
                renderJson("修改成功",1);
            }else{
                $openid = Db::name("wx_users")->where("uid",$uid)->value("openid");
                $msg = [
                    "condition"=>6,
                    "touser"=>$openid,
                    "content"=>[$this->userInfo["username"]]
                ];
                $MiniMsg = new  \app\common\lib\MiniMsg($msg);
                $MiniMsg->sendMsg();//发送短信模板银行卡修改成功
                renderJson("修改失败",1);

            }

        }catch (\Exception $e) {
            $openid = Db::name("wx_users")->where("uid",$uid)->value("openid");
            $msg = [
                "condition"=>6,
                "touser"=>$openid,
                "content"=>[$this->userInfo["username"]]
            ];
            $MiniMsg = new  \app\common\lib\MiniMsg($msg);
            $MiniMsg->sendMsg();//发送短信模板银行卡修改成功
            renderJson("修改失败",0);

        }


    }

















}